#coding=utf8

from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField, SubmitField, DateField, TextAreaField, FileField, RadioField
from wtforms.validators import DataRequired, Regexp, EqualTo, ValidationError, URL
from ..models import db, Publisher

class PublisherRegisterForm(FlaskForm):
	organization_name = StringField(
		label=u'组织名称',
		validators=[
			DataRequired(u'组织名称不能为空')
		],
		description=u'组织名称',
		render_kw={"class": "form-control", "placeholder": u"单行输入"}
	)
	official_accounts_name = StringField(
		label=u'公众号名称',
		validators=[
			DataRequired(u'公众号名称不能为空')
		],
		description=u'公众号名称',
		render_kw={"class": "form-control", "placeholder": u"单行输入"}
	)
	person_name = StringField(
		label=u'负责人姓名',
		validators=[
			DataRequired(u'负责人姓名不能为空')
		],
		description=u'负责人姓名',
		render_kw={"class": "form-control", "placeholder": u"单行输入"}
	)
	person_num = StringField(
		label=u'负责人学号（即登录账号）',
		validators=[
			DataRequired(u'负责人学号不能为空')
		],
		description=u'负责人学号（即登录账号）',
		render_kw={"class": "form-control", "placeholder": u"单行输入", "id": "stu_num"}
	)
	person_wechat_id = StringField(
		label=u'负责人微信',
		validators=[
			DataRequired(u'负责人微信不能为空')
		],
		description=u'负责人微信',
		render_kw={"class": "form-control", "placeholder": u"单行输入"}
	)
	phone_num = StringField(
		label=u'负责人电话',
		validators=[
			DataRequired(u'负责人电话不能为空'),
			Regexp('(((13[0-9])|(14[57])|(15[0-35-9])|(17[36-8])|(18[0-9]))\\d{8})|(170[057-9]\\d{7})', message=u'请输入正确的手机号码')
		],
		description=u'负责人电话',
		render_kw={"class": "form-control", "placeholder": u"单行输入"}
	)
	password = PasswordField(
		label=u'密码',
		validators=[
			DataRequired(u'密码不能为空')
		],
		description=u'密码',
		render_kw={"class": "form-control", "placeholder": u"请输入密码"}
	)
	repeat_password = PasswordField(
		label=u'确认密码',
		validators=[
			DataRequired(u'确认密码不能为空'),
			EqualTo('password', message=u'两次密码输入不一致')
		],
		description=u'确认密码',
		render_kw={"class": "form-control", "placeholder": u"请再次输入密码"}
	)
	submit = SubmitField(
		u'提交',
		render_kw={"class": "btn btn-success", "style": "background: #007bff"}
	)

	def validate_person_num(self, field):
		person_num = field.data
		count = Publisher.query.filter_by(person_num=person_num).count()
		if count > 0:
			raise ValidationError(u'该账号已经存在，不能重复注册')



class LoginForm(FlaskForm):
	person_num = StringField(
		label=u'负责人学号（即登录账号）',
		validators=[
			DataRequired(u'学号不能为空')
		],
		description=u'负责人学号（即登录账号）',
		render_kw={"class": "form-control", "placeholder": u"单行输入", "id": "stu_num"}
	)
	password = PasswordField(
		label=u'密码',
		validators=[
			DataRequired(u'密码不能为空')
		],
		description=u'密码',
		render_kw={"class": "form-control", "placeholder": u"请输入密码"}
	)
	submit = SubmitField(
		u'登录',
		render_kw={"class": "btn btn-success", "style": "background: #007bff; margin-left: 50px"}
	)


	def validate_password(self, field):
		publisher = Publisher.query.filter_by(person_num=self.person_num.data).first()
		# print publisher
		if not publisher:
			raise ValidationError(u'用户名或密码错误，请重新登录')
		if not publisher.checkpassword(field.data):
			raise ValidationError(u'用户名或密码错误，请重新登录。')


class ActivityForm(FlaskForm):
	title = StringField(
		label=u'活动标题',
		validators=[
			DataRequired(u'活动标题不能为空')
		],
		description=u'活动标题',
		render_kw={"class": "form-control", "placeholder": u"单行输入"}
	)
	closing_date = DateField(
		label=u'截止日期',
		validators=[
			DataRequired(u'截止日期不能为空')
		],
		description=u'截止日期',
		render_kw={"class":"form-control", "type": "date"}
	)
	description = TextAreaField(
		label=u'活动描述',
		validators=[
			DataRequired(u'活动描述不能为空')
		],
		description=u'活动描述',
		render_kw={"class": "form-control", "placeholder": u"请用不超过50字概述你的活动", "style": "height:120px;"}
	)
	activity_class = RadioField(
		u'活动分类',
	    choices=[
			(u'0', u'校精选'),
			(u'1', u'悦青春'),
			(u'2', u'闯未来'),
			(u'3', u'看世界'),
		    (u'4', u'秀自我'),
		    (u'5', u'知天下')
		],
		default=u'0'
	)
	logo = FileField(
		label=u'展示图片',
		validators=[
			DataRequired(u'展示图片不能为空')
		],
		render_kw={"class": "form-control-file", "accept": "image/*"}
	)
	article_url = StringField(
		label=u'文章链接',
		validators=[
			DataRequired(u'文章链接不能为空'),
			URL(u'请输入正确的HTTP链接')
		],
		description=u'文章链接',
		render_kw={"class":"form-control", "placeholder": u"单行输入"}
	)
	remark = TextAreaField(
		label=u'备注',
		description=u'备注',
		render_kw={"class": "form-control", "placeholder": u"选填", "style": "height:60px;"}
	)
	submit = SubmitField(
		u'提交',
		render_kw={"class": "btn btn-success", "style": "background: #007bff; margin-left: 50px"}
	)

	def validate_article_url(self, field):
		url = field.data
		# print publisher
		if 'weixin.qq.com' not in url:
			raise ValidationError(u'请输出正确的微信文章链接')
